module.exports = {
	/**
	 * 查询角色列表
	 * @url client/wechat/selects/pub/selects 前端调用的url参数地址
	 * 表名
	 * @dbName 查询的表的名称
	 * data 请求参数 说明
	 * @param {String}         _id  用户编号
	 * res 返回参数说明
	 * @param {Number}         code      错误码，0表示成功
	 * @param {String}         msg       详细信息
	 */
	main: async (event) => {
		let { data = {}, userInfo, util, filterResponse, originalParam } = event;
		let { customUtil, uniID, config, pubFun, vk, db, _ } = util;
		let { uid } = data;
		let res = { code: 0, msg: '' };
		// 业务逻辑开始-----------------------------------------------------------
		let {user_id, detail_id, type, origin, index} = data;
		
		let plaform = ''
		if(originalParam.context.PLATFORM === 'mp-weixin'){
			plaform = 'wx'
		}
		// 下载配置
		let client = await vk.baseDao.findByWhereJson({
			dbName: 'bus-wx-config',
			whereJson: {
				config_key: plaform+'_config'
			}
		})
		let score = 0,image = '',video = '',comment = '',detail = {}, mode = 0, author_id = ''
		if(type === 'widget'){
			detail = await vk.baseDao.findById({
				dbName: 'bus-wx-widget',
				id: detail_id
			})
		}else if(type != 'design'){
			detail = await vk.baseDao.findById({
				dbName: 'bus-wx-cover',
				id: detail_id
			})
			if(!detail){
				return {
					code: -1,
					msg:"该作品不存在"
				}
			}
			mode = detail.mode
			author_id = detail.user_id
		}
		if(type === 'detail'){
			if(origin === 'score') score = -client.down_score
			image = detail.images[index]
			comment = '下载壁纸：'+detail.name
		}else if(type === 'avatar'){
			if(origin === 'score') score = -client.avatar_score
			image = detail.images[index]
			comment = '下载头像：'+detail.name
		}else if(type === 'look'){
			if(origin === 'score') score = -client.look_score
			image = detail.images[index]
			comment = '下载表情包：'+detail.name
		}else if(type === 'trends'){
			if(origin === 'score') score = -client.downs_score
			image = detail.video+'?x-oss-process=video/snapshot,t_1000,m_fast'
			video = detail.video
			comment = '下载视频：'+detail.name
		}else if(type === 'design'){
			if(origin === 'score') score = -client.fringe_score
			comment = '制作刘海壁纸'
			mode = 3
		}else if(type === 'widget'){
			if(origin === 'score') score = -client.widget_score
			image = detail.image
			comment = '制作头像挂件'
			mode = 4
		}
		
		if(score != 0){
			//修改用户信息
			var userData = await vk.baseDao.updateAndReturn({
				dbName: 'uni-id-users',
				whereJson:{
					_id: user_id,
					score: _.gte(score*-1)
				},
				dataJson:{
					score: _.inc(score)
				}
			});
			if(!userData){
				return {
					code: -1,
					msg: "余额不足"
				}
			}
			res.user = userData
			
			//添加积分记录
			var scoreNum = await vk.baseDao.add({
				dbName: "uni-id-scores",
				dataJson: {
					user_id: user_id,
					type: score>0?1:2,
					score: score>0?score:-score,
					balance: userData.score,
					comment: comment
				}
			});
		}
		
		let newDate = vk.pubfn.timeFormat(new Date(),"yyyy-MM-dd");
		if(origin === 'ad'){
			origin = plaform+'_'+origin
			if(author_id){
				// 查询作者信息
				let userInfo = await vk.baseDao.findById({
					dbName:"uni-id-users",
					id: author_id
				})
				if(origin.indexOf(userInfo.origin) === -1){
					origin = origin+'→'+userInfo.origin
				}
			}
		}
		
		//添加订单记录
		var scoreNum = await vk.baseDao.add({
			dbName: "bus-wx-order",
			dataJson: {
				author_id,
				user_id,
				origin,
				detail_id,
				video,
				image,
				score: -score,
				mode,
				comment,
				day: newDate
			}
		});
		res.score = score
		
		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}

}